from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import declarative_base
from sqlalchemy import engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

# 创建数据库连接
engine = create_engine('mysql+pymysql://root:zzh0117.@113.31.110.212:3306/db10?charset=utf8')

Base = declarative_base()
class User(Base):
    __tablename__ = 'user'
    uid = Column(Integer, primary_key=True)
    username = Column(String(50))
    password = Column(String(50))
    email = Column(String(50))
    tel = Column(String(50))

    def __repr__(self):
        return f"User(uid={self.uid}, username={self.username}, password={self.password}, email={self.email}, tel={self.tel})"


class QA(Base):
    __tablename__ = 'qa'
    qid = Column(Integer, primary_key=True)
    question = Column(String(255))
    answer = Column(String(255))



def load_qa():
    session = sessionmaker(bind=engine)
    s = session()
    qa = s.query(QA).all()
    qa = [{"question":q.question,"answer":q.answer} for q in qa]
    s.close()
    return qa


if __name__ == "__main__":
    print(load_qa())